<?php

//产品添加后置方法
function Product_after_deal($table,$id){
	
	$model = M("$table");
	$row   = $model->find($id);
	
	//处理关联商品
	$to_select = $_POST['to_select'];
	$relation  = M("ProductRelation");
	$relation->where(array('pid'=>$id))->delete();
	if(is_array($to_select)){
		//去掉重复的产品
		//$to_select = array_unique($to_select);
		$time = time();
		foreach($to_select as $v){
			$res = explode(",",$v);
			if(intval($res[0])){
				$data['pid']    = $id;
				$data['tid']    = $res[0];
				$data['rprice'] = doubleval($res[1]);
				$data['addtime']= $time;
				$data['addid']  = session(C('USER_AUTH_KEY'));;
				$relation->add($data);
			}
		}
	}
	
	//处理关联文章
	$to_article = $_POST['to_article'];
	$article    = M("ProductArticle");
	$article->where(array('pid'=>$id))->delete();
	if(is_array($to_article)){

		$time = time();
		foreach($to_article as $v){

			$data['pid']    = $id;
			$data['tid']    = $v;
			$data['addtime']= $time;
			$data['addid']  = session(C('USER_AUTH_KEY'));;
			$article->add($data);
		}
	}
	
	$module = $row["module"];
	if($module){
		$model = D("Module");
		$mid   = $model->getMoudle($module);
		$filed = $model->getModuleField($mid);
		
		$subm  = M("$module");
		$map['solevar'] = $row["solevar"];
		$one = $subm->where($map)->find();
		
		$subm->create();
		$subm->cmodule = $module;
		$subm->addtime = time();
		$subm->addid   = session(C('USER_AUTH_KEY'));
		//dump($subm->data());
		if($one['id']){
			$subm->id = $one['id'];
			$subm->save();
		}else{
			$subm->add();
		}
	}
}

//门店后置操作-更新百度缓存
function Outlet_after_deal($table,$id,& $url){
		
	$model = M("$table");
	$data  = $model->field("mapid,title,longitude,latitude,area,address,tags,tel,city")->find($id);
	$data["outletid"] = $id;

	if($data["mapid"]){
		$data["id"]   = $data["mapid"];
		$json = mapStorge($data,"update");
	}else{
		$json = mapStorge($data,"create");
		$mapid= $json['id'] ;
		$model->save(array("id"=>$id,"mapid"=>$mapid));
	}
	
	if($json['status'] === 0){
		mapAll(true);
	}else{
		//$this->assign("jumpUrl",U("Outlet/edit","id=$id"));
		//$this->error("标记门店地址出错，请重新标记");
	}
}

//品牌后置操作-更新缓存文件
function Brands_after_deal($table,$id){
	$map['isshow'] = 1;
	$data = M("$table")->field('id,title')->where($map)->order("sort desc")->select();
	
	foreach($data as $v){
		$list[$v['id']] = $v;
	}
	$key = "Brands"; 
	F($key,$list);
}

//碎片前置方法
function Block_before($model){
	
	$value = I("post.value");
	$json  = json_encode($value);
	$key   = $model->key;
	$path  = DATA_PATH."Block/";
	F("$key",$value,$path);
	
	$model->value = $json;
	return $model;
}